"""
Case Type   : cm_agent参数
Case Name   : 查询并使用cm_ctl方式修改参数log_dir默认值
Create At   : 2025/3/20
Owner       : ningyali
Description :
    1.查询参数log_dir默认值
    2.修改日志路径
    3.重启集群
    4.查看log_dir参数值
    5.查看对应路径下是否生成日志
    6.恢复参数默认值
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功
    6.执行成功
History     :
    2024.4.15 ningyali 增加前置（安装cm）和后置（卸载cm）操作
"""

import os
import time
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CMServer(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.common = Common()
        self.new_path = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH), 'gaussdb_log',
            self.user_nodes[0].ssh_user, 'cm', 'cm_agent_1')

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_trans(self):
        text = '----step1:查询参数log_dir默认值;expect:执行成功----'
        self.log.info(text)
        q_logdir_cmd = f"source {macro.DB_ENV_PATH};" \
                       f"cm_ctl list --param --agent | grep log_dir"
        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn('cm/cm_agent', q_res, f'执行失败: {text}')

        text = '----step2:修改日志路径;expect:执行成功----'
        self.log.info(text)
        set_cmd = f"source {macro.DB_ENV_PATH};" \
                  f"cm_ctl set --param --agent -k " \
                  f"log_dir=\"'{self.new_path}'\";"
        set_res = self.common.get_sh_result(self.user_nodes[0], set_cmd)
        self.assertIn('set cm_agent.conf success', set_res, f'执行失败: {text}')

        text = '----step3:重启集群;expect:执行成功----'
        self.log.info(text)
        restart_cmd = f"source {macro.DB_ENV_PATH};cm_ctl stop && cm_ctl start"
        restart_res = self.common.get_sh_result(self.user_nodes[0], restart_cmd)
        self.assertEqual(restart_res.count('cluster successfully'), 2,
                         f'执行失败: {text}')
        time.sleep(5)

        text = '----step4:查看log_dir参数值;expect:执行成功----'
        self.log.info(text)
        q_res = self.common.get_sh_result(self.user_nodes[0], q_logdir_cmd)
        self.assertIn(self.new_path, q_res, f'执行失败: {text}')

        text = '----step5:查看对应路径下是否生成日志;expect:执行成功----'
        self.log.info(text)
        ls_cmd = f'ls {self.new_path}'
        ls_res = self.common.get_sh_result(self.user_nodes[0], ls_cmd)
        expect = (r'cm_agent-.*-current.log', 'pg_log',
                  r'system_alarm-.*-current.log', 'system_call-current.log')
        for e in expect:
            self.assertRegex(ls_res, e, f'执行失败: {text}-{e}')

        text = '----step6:恢复参数默认值;expect:执行成功----'
        self.log.info(text)
        set_res = self.common.get_sh_result(
            self.user_nodes[0], set_cmd.replace('cm_agent_1', 'cm_agent'))
        self.assertIn('set cm_agent.conf success', set_res, f'执行失败: {text}')

    def tearDown(self):
        text = '----poststep1:卸载cm;expect:执行成功----'
        self.log.info(text)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.pri_sh.node.ssh_user)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0], '--deleteData --deleteBinary')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
